package scrum.gui.project;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.*;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.text.DateFormatter;
import scrum.entity.*;


public class GeneralPane extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3747377848146255206L;

	
	JFrame jFrame = null;
	private boolean bEditable;
	
	public Project currentProject;
	
	public UserStory currentStory;
	
	private ArrayList<UserStory> userStory;
	
	/** Creates new form GeneralPropPane */
	public GeneralPane() {
		
		initComponents();
	}
	
	public void setCurrentProject(Project currentProject){
		
		this.currentProject = currentProject;
		this.userStory = currentProject.getUserStorys();
		if(!userStory.isEmpty()){
			this.currentStory = userStory.get(0);
		}else{
			currentStory = new UserStory();
		}
		
	}
	

	public void setModifyMode(boolean bEditableIn) {
		
		bEditable = bEditableIn;

		jTextFieldProjectName.setEditable(bEditable);
		jTextFieldProjectOwner.setEditable(bEditable);
		jTextFieldScrumMaster.setEditable(bEditable);
		jTextFieldDeveloper.setEditable(bEditable);
		jTextFieldTester.setEditable(bEditable);
		jTextAreaDescription.setEditable(bEditable);
		jTextFieldAnalyst.setEditable(bEditable);
		jTextFieldInterfaceDesigner.setEditable(bEditable);
		startDate.setEditable(bEditable);
		dueDate.setEditable(bEditable);
		

		if(currentStory.getStory() == null){
			
			this.jButtonDeleteStory.setEnabled(false);
			this.jButtonModifyStory.setEnabled(false);
		}else{
			jButtonModifyStory.setEnabled(bEditable);
			jButtonDeleteStory.setEnabled(bEditable);
		}
		
		jButtonAddNewStory.setEnabled(bEditable);
		
	}

	public void addNewUserStory(UserStory story){
		
		userStory.add(story);
		addNode(story);	
		
	}

	private void deleteUserStory() {
		
		int index = getIndex(currentStory);
		
		userStory.remove(index);
		removeCurrentNode();
		if(!userStory.isEmpty()){
			this.currentStory = userStory.get(0);
		}else{
			currentStory = new UserStory();
		}
		clearStoryDetails();
		showStoryDetails();
	}
	
	private void modifyUserSotry(){
		
		if(currentStory!=null){
			
			UserStoryDialog dlg = new UserStoryDialog(this,true,2);
			dlg.setVisible(true);
		}
	}
	
	public void saveStoryModification(UserStory story,int index){
		
		userStory.set(index, story);
		rootTreeNode.removeAllChildren();
		createUserStoryTree();
        treeModel.reload();
		
	}
	
	public void setDate(){
		startDate.setValue(new Date());
		dueDate.setValue(new Date());
	}

	
	public void clearProject() {
		
		jTextFieldProjectName.setText("");
		jTextFieldProjectOwner.setText("");
		jTextFieldScrumMaster.setText("");
		jTextFieldDeveloper.setText("");
		jTextFieldTester.setText("");
		jTextAreaDescription.setText("");
		jTextFieldAnalyst.setText("");
		jTextFieldInterfaceDesigner.setText("");
		startDate.setText("");
		dueDate.setText("");
		
		clearStoryDetails();
		clearNodes();
		
	}
	

	public void showProject(){
		
		clearProject();
		
		jTextFieldProjectName.setText(currentProject.getProjectName());
		jTextFieldProjectOwner.setText(currentProject.getProjectOwner());
		jTextFieldScrumMaster.setText(currentProject.getScrumMaster());
		jTextFieldDeveloper.setText(currentProject.getDeveloper());
		jTextFieldTester.setText(currentProject.getTester());
		jTextAreaDescription.setText(currentProject.getDescription());
		jTextFieldAnalyst.setText(currentProject.getAnalyst());
		jTextFieldInterfaceDesigner.setText(currentProject.getInterfacer());
		startDate.setText(currentProject.getStartDate());
		dueDate.setText(currentProject.getDueDate());
		
		createUserStoryTree();
		treeModel.reload();
		showStoryDetails();
		
		
	}
	
	public void clearStoryDetails(){
		
		this.jTextFieldResponsible.setText("");
		this.progressBar.setValue(0);
		this.startDateUserStory.setText("");
		this.dueDateUserStory.setText("");
		this.jTextFieldBussinessValue.setText("");
		this.jTextFieldWeight.setText("");
		
	}
	
	public void showStoryDetails(){
		
		this.jTextFieldResponsible.setText(currentStory.getResponsible());
		this.progressBar.setValue(currentStory.getProgress());
		this.startDateUserStory.setText(currentStory.getStartDate());
		this.dueDateUserStory.setText(currentStory.getDueDate());
		this.jTextFieldBussinessValue.setText(currentStory.getBusinessValue());
		this.jTextFieldWeight.setText(currentStory.getWeight());
	}

	public void getInput(Project project){
		
		project.setProjectName(jTextFieldProjectName.getText());
		project.setProjectOwner(jTextFieldProjectOwner.getText());
		project.setScrumMaster(jTextFieldScrumMaster.getText());
		project.setDeveloper(jTextFieldDeveloper.getText());
		project.setTester(jTextFieldTester.getText());
		project.setInterfacer(jTextFieldInterfaceDesigner.getText());
		project.setDescription(jTextAreaDescription.getText());
		project.setAnalyst(jTextFieldAnalyst.getText());
		project.setStartDate(startDate.getText());
		project.setDueDate(dueDate.getText());
	}

	
	
	public void createUserStoryTree(){
		
		DefaultMutableTreeNode top = rootTreeNode;
		DefaultMutableTreeNode project = null;
		if(userStory != null){
			for (int i = 0; i < userStory.toArray().length; i++){
	 			project = new DefaultMutableTreeNode(userStory.get(i));
	 			top.add(project);
	 		}
		}
	}
	
	
	public int getIndex(UserStory story){
    	 
    	 int index = -1;
    	 for (int i = 0; i < userStory.toArray().length; i++) {
    		 
    		 if(userStory.get(i).getStory().equals(story.getStory())){
    			 index = i;
    			 break;
    		 }
 		}
    	return index;
     }

    /** Remove all nodes except the root node. */
    public void clearNodes() {
        rootTreeNode.removeAllChildren();
        treeModel.reload();
    }

    /** Remove the currently selected node. */
    public void removeCurrentNode() {
        TreePath currentSelection = jTreeUserStory.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
                         (currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
            }
        } 

    }
  
    //add new object to the tree
    public void addNode( UserStory userStory) {
        DefaultMutableTreeNode childNode = 
                new DefaultMutableTreeNode(userStory);

        treeModel.insertNodeInto(childNode, rootTreeNode, 
        		rootTreeNode.getChildCount());

        // Make sure the user can see the lovely new node.
        jTreeUserStory.scrollPathToVisible(new TreePath(childNode.getPath()));
        
    }
    
	
	
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		java.awt.GridBagConstraints gridBagConstraints;

		jPanelProjectGeneral = new javax.swing.JPanel();
		
		jLabelProjectName = new javax.swing.JLabel();
		jTextFieldProjectName = new javax.swing.JTextField();
		jLabelProjectOwner = new javax.swing.JLabel();
		jTextFieldProjectOwner = new javax.swing.JTextField();
		
		jLabelScrumMaster = new javax.swing.JLabel();
		jTextFieldScrumMaster = new javax.swing.JTextField();
		jLabelDeveloper = new javax.swing.JLabel();
		jTextFieldDeveloper = new javax.swing.JTextField();
		jLabelTester = new javax.swing.JLabel();
		jLabelAnalyst = new javax.swing.JLabel();
		jLabelInterfaceDesigner = new javax.swing.JLabel();
		jTextFieldTester = new javax.swing.JTextField();
		jTextFieldAnalyst = new javax.swing.JTextField();
		jTextFieldInterfaceDesigner = new javax.swing.JTextField();
		jLabelStartDate = new javax.swing.JLabel();
		jLabelDueDate = new javax.swing.JLabel();
		jPanelDescription = new javax.swing.JPanel();
		jScrollPaneDescription = new javax.swing.JScrollPane();
		jPanelUserStory = new javax.swing.JPanel();
		jScrollPaneUserStory = new javax.swing.JScrollPane();
		jPanelDetails = new javax.swing.JPanel();
		
		jButtonDeleteStory = new javax.swing.JButton();
		jButtonModifyStory = new javax.swing.JButton();
		jButtonAddNewStory = new javax.swing.JButton();
		
		jLabelResponsible = new javax.swing.JLabel();
		jLabelProgress = new javax.swing.JLabel();
		jLabelStratUserStory = new javax.swing.JLabel();
		jLabelDueUserSotry = new javax.swing.JLabel();
		jLabelBussinessValue = new javax.swing.JLabel();
		jLabelWeight = new javax.swing.JLabel();
		progressBar = new JProgressBar(0, 100);
		progressBar.setStringPainted(Boolean.TRUE);
		jTextFieldResponsible = new javax.swing.JTextField();
		jTextFieldBussinessValue = new javax.swing.JTextField();
		jTextFieldWeight = new javax.swing.JTextField();
		

		dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(true);
		startDateUserStory = new javax.swing.JFormattedTextField(dateFormat);
		dueDateUserStory = new javax.swing.JFormattedTextField(dateFormat);
		startDate = new javax.swing.JFormattedTextField(dateFormat);
		dueDate = new javax.swing.JFormattedTextField(dateFormat);
		
		//((DateFormatter)startDate.getFormatter()).setAllowsInvalid(false);
		
		jTextAreaDescription = new javax.swing.JTextArea();
		
		

        rootTreeNode = new DefaultMutableTreeNode("UserStory");
       
        treeModel = new DefaultTreeModel(rootTreeNode);
		jTreeUserStory = new javax.swing.JTree(treeModel);
		
		jTreeUserStory.setEditable(false);
		jTreeUserStory.setRootVisible(false);
		jTreeUserStory.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		((DefaultTreeCellRenderer) jTreeUserStory.getCellRenderer())
		.setLeafIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/scrum/resource/interface.gif")));
		jTreeUserStory.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
            	treeSelectionChanged(evt);
            }
        });

		setLayout(new java.awt.GridBagLayout());

		// Start of Project General

		jPanelProjectGeneral.setLayout(new java.awt.GridBagLayout());
		
		jPanelProjectGeneral.setBorder(new javax.swing.border.TitledBorder(
				null, "Project general", javax.swing.border.TitledBorder.LEFT,
				javax.swing.border.TitledBorder.DEFAULT_POSITION));

		gridBagConstraints = new java.awt.GridBagConstraints();
		
		
		jLabelProjectName.setText("Name");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		jPanelProjectGeneral.add(jLabelProjectName, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		jPanelProjectGeneral.add(jTextFieldProjectName, gridBagConstraints);

		jLabelProjectOwner.setText("Owner");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		jPanelProjectGeneral.add(jLabelProjectOwner, gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		jPanelProjectGeneral.add(jTextFieldProjectOwner, gridBagConstraints);

		jLabelScrumMaster.setText("Master");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		jPanelProjectGeneral.add(jLabelScrumMaster, gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		jPanelProjectGeneral.add(jTextFieldScrumMaster, gridBagConstraints);

		jLabelDeveloper.setText("Employee1");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		jPanelProjectGeneral.add(jLabelDeveloper, gridBagConstraints);


		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		jPanelProjectGeneral.add(jTextFieldDeveloper, gridBagConstraints);

		jLabelTester.setText("Employee2");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		jPanelProjectGeneral.add(jLabelTester, gridBagConstraints);


		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		jPanelProjectGeneral.add(jTextFieldTester, gridBagConstraints);

		jLabelAnalyst.setText("Employee3");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 5;
		jPanelProjectGeneral.add(jLabelAnalyst, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 5;
		jPanelProjectGeneral.add(jTextFieldAnalyst, gridBagConstraints);

		jLabelInterfaceDesigner.setText("Employee4");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 6;
		jPanelProjectGeneral.add(jLabelInterfaceDesigner, gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 6;
		jPanelProjectGeneral.add(jTextFieldInterfaceDesigner, gridBagConstraints);


		jLabelStartDate.setText("StartDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 7;
		jPanelProjectGeneral.add(jLabelStartDate, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 7;
		jPanelProjectGeneral.add(startDate, gridBagConstraints);

		jLabelDueDate.setText("DueDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 8;
		jPanelProjectGeneral.add(jLabelDueDate, gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 8;
		jPanelProjectGeneral.add(dueDate, gridBagConstraints);

		jPanelDescription.setLayout(new java.awt.GridBagLayout());
		jPanelDescription.setBorder(new javax.swing.border.TitledBorder(
				"Description"));

		jScrollPaneDescription.setViewportView(jTextAreaDescription);
		jTextAreaDescription.setLineWrap(true);
		jTextAreaDescription.setWrapStyleWord(true);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		jPanelDescription.add(jScrollPaneDescription, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 9;
		jPanelProjectGeneral.add(jPanelDescription, gridBagConstraints);

		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 0.4;
		gridBagConstraints.weighty = 1.0;
		add(jPanelProjectGeneral, gridBagConstraints);

		// End of Project General

		
		
		
		// Start of UserStory

		jPanelUserStory.setLayout(new java.awt.GridBagLayout());
		jPanelUserStory.setBorder(new javax.swing.border.TitledBorder(
				"User Story"));
		

		jScrollPaneUserStory.setViewportView(jTreeUserStory);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		jPanelUserStory.add(jScrollPaneUserStory, gridBagConstraints);
		
		
		
		jPanelDetails.setLayout(new java.awt.GridBagLayout());
		jPanelDetails.setBorder(new javax.swing.border.TitledBorder(
				"StoryDetails"));
		gridBagConstraints = new java.awt.GridBagConstraints();
		
		jLabelResponsible.setText("Responsible");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelResponsible,gridBagConstraints);

		
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 1;
		jTextFieldResponsible.setEditable(false);
		jPanelDetails.add(jTextFieldResponsible, gridBagConstraints);
		
		
		
		
		jLabelProgress.setText("Progress");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelProgress, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.weightx = 0.3;
		jPanelDetails.add(progressBar, gridBagConstraints);
		
		jLabelStratUserStory.setText("StartDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelStratUserStory, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 1;
		startDateUserStory.setEditable(false);
		jPanelDetails.add(startDateUserStory, gridBagConstraints);
		
		
		
		jLabelDueUserSotry.setText("DueDate");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelDueUserSotry, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.weightx = 0.3;
		dueDateUserStory.setEditable(false);
		jPanelDetails.add(dueDateUserStory, gridBagConstraints);
		

		jLabelBussinessValue.setText("BussinessValue");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelBussinessValue, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 1;
		jTextFieldBussinessValue.setEditable(false);
		jPanelDetails.add(jTextFieldBussinessValue, gridBagConstraints);
		
		jLabelWeight.setText("WeightValue");
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0;
		jPanelDetails.add(jLabelWeight, gridBagConstraints);

		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.weightx = 0.3;
		jTextFieldWeight.setEditable(false);
		jPanelDetails.add(jTextFieldWeight, gridBagConstraints);
		
		
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		jPanelUserStory.add(jPanelDetails, gridBagConstraints);
		

		jButtonDeleteStory.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/scrum/resource/close1.gif")));
		jButtonDeleteStory.setText("Delete");
		jButtonDeleteStory
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						btnDeleteUserStoryActionPerformed(evt);
					}
				});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		jPanelUserStory.add(jButtonDeleteStory, gridBagConstraints);
		
		
		jButtonModifyStory.setText("Modify");
		jButtonModifyStory
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						btnModifyUserStoryActionPerformed(evt);
					}
				});
		
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		jPanelUserStory.add(jButtonModifyStory, gridBagConstraints);
		
		jButtonAddNewStory.setText("Add New");
		jButtonAddNewStory
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						btnAddNewUserStoryActionPerformed(evt);
					}
				});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		jPanelUserStory.add(jButtonAddNewStory, gridBagConstraints);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		add(jPanelUserStory, gridBagConstraints);

	}

	private void treeSelectionChanged(TreeSelectionEvent evt) {

		DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTreeUserStory
				.getLastSelectedPathComponent();

		if (node == null)
			return;
		Object nodeInfo = node.getUserObject();
		if (node.isLeaf()) {
			currentStory = (UserStory) nodeInfo;
			clearStoryDetails();
			showStoryDetails();
		}
	}

	//This will run when Delete UserStory Button has been pressed 
	private void btnDeleteUserStoryActionPerformed(
			java.awt.event.ActionEvent evt) {
		deleteUserStory();
	}

	//This will run when Add UserStory Button has been pressed 
	private void btnAddNewUserStoryActionPerformed(
			java.awt.event.ActionEvent evt) {
		
		UserStoryDialog dlg = new UserStoryDialog(this,true,1);
		dlg.setVisible(true);
	}
	
	//This will run when Modify UserStory Button has been pressed 
	private void btnModifyUserStoryActionPerformed(
			java.awt.event.ActionEvent evt) {
		modifyUserSotry();
	}


	// Variables declaration 
	private javax.swing.JButton jButtonAddNewStory;
	private javax.swing.JButton jButtonDeleteStory;
	private javax.swing.JButton jButtonModifyStory;
	private javax.swing.JPanel jPanelProjectGeneral;
	private javax.swing.JPanel jPanelUserStory;
	private javax.swing.JPanel jPanelDescription;
	private javax.swing.JPanel jPanelDetails;
	private javax.swing.JScrollPane jScrollPaneDescription;
	private javax.swing.JScrollPane jScrollPaneUserStory;
	private javax.swing.JLabel jLabelProjectName;
	private javax.swing.JLabel jLabelScrumMaster;
	private javax.swing.JLabel jLabelDeveloper;
	private javax.swing.JLabel jLabelTester;
	private javax.swing.JLabel jLabelAnalyst;
	private javax.swing.JLabel jLabelInterfaceDesigner;
	private javax.swing.JLabel jLabelStartDate;
	private javax.swing.JLabel jLabelDueDate;
	private javax.swing.JLabel jLabelProjectOwner;
	
	private javax.swing.JLabel jLabelResponsible;
	private javax.swing.JLabel jLabelProgress;
	private javax.swing.JLabel jLabelStratUserStory;
	private javax.swing.JLabel jLabelDueUserSotry;
	private javax.swing.JLabel jLabelBussinessValue;
	private javax.swing.JLabel jLabelWeight;
	
	private JFormattedTextField startDateUserStory;
	private JFormattedTextField dueDateUserStory;
	private JProgressBar progressBar;
	private javax.swing.JTextField jTextFieldResponsible;
	private javax.swing.JTextField jTextFieldBussinessValue;
	private javax.swing.JTextField jTextFieldWeight;
	
	private javax.swing.JTree jTreeUserStory;
	private javax.swing.JTextField jTextFieldProjectName;
	private javax.swing.JTextField jTextFieldScrumMaster;
	private javax.swing.JTextField jTextFieldDeveloper;
	private javax.swing.JTextField jTextFieldTester;
	private javax.swing.JTextField jTextFieldAnalyst;
	private javax.swing.JTextField jTextFieldInterfaceDesigner;
	private javax.swing.JTextField jTextFieldProjectOwner;
	private JFormattedTextField startDate;
	private JFormattedTextField dueDate;
	private DateFormat dateFormat;
	private JTextArea jTextAreaDescription;
	private DefaultMutableTreeNode rootTreeNode;
    private DefaultTreeModel treeModel;

}
